package com.decoration.basic;

import com.decoration.basic.entity.FindCondition;
import com.decoration.basic.entity.JsonResult;

public class BasicController {
	private static final String ADMIN_PATH = "WEB-INF/backend/";
	private static final String FRONT_PATH = "view/";

	/**
	 * 返回后台视图地址
	 * @param viewName 视图名称
	 * @return 组装后的后台视图地址
	 */
	protected String M(String viewName) {
		return ADMIN_PATH + viewName;
	}

	/**
	 * 返回前台视图地址
	 *
	 * @param viewName 视图名称
	 * @return 组装后的前台视图地址
	 */
	protected String F(String viewName) {
		return FRONT_PATH + viewName;
	}

	/**
	 * 返回前台失败信息
	 */
	protected JsonResult Faild(String msg) {
		JsonResult result = new JsonResult();
		result.setCode(JsonResult.FAILED_CODE);
		result.setMsg(msg);
		return result;
	}

	/**
	 * 返回前台失败信息
	 */
	protected JsonResult Sucess(Integer count,String msg, Object obj) {
		JsonResult result = new JsonResult();
		result.setCode(JsonResult.SUCCESS_CODE);
		result.setCount(count);
		result.setMsg(msg);
		result.setData(obj);
		return result;
	}

	/**
	 * 分页数据量,默认10
	 * 
	 * @param limit 每页数据量
	 * @return
	 */
	private Integer getPageDataCount(Integer limit) {
		return limit == null ? 10 : limit;
	}

	/**
	 * 分页数据游标,默认0
	 * 
	 * @param page  当前页
	 * @param limit 每页数据量
	 * @return 当前页数据游标
	 */
	private Integer getPageDataIndex(Integer page, Integer limit) {
		Integer count = limit == null ? 10 : limit;
		if (page == null || page <= 0) {
			return 0;
		}
		return (page-1) * count;
	}

	/**
	 *  设置分页参数查询
	 * @param condition
	 */
	protected void setPagerInfo(FindCondition condition) {
		condition.setPage(condition.getPage()==null ? 1 : condition.getPage());
		condition.setLimit(getPageDataCount(condition.getLimit()));
		condition.setDataIndex(getPageDataIndex(condition.getPage(), condition.getLimit()));
		condition.setDataCount(getPageDataCount(condition.getLimit()));
	}

}
